package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.WaresDao;
import dao.impl.IWaresDao;
import entity.Wares;

public class WaresListServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
			int page = 1;
			int pagesize=4;
			
			String pages = request.getParameter("page");
			if (pages!=""&&pages!=null) {
				page = Integer.parseInt(pages);
			}
			String type="";
			String types = request.getParameter("type");
			if (types!=""&&types!=null) {
				type =new String(types.getBytes("iso-8859-1"),"utf-8");
			}
			
			
			WaresDao wd = new IWaresDao();
			List<Wares> list = wd.FindWares(page, pagesize, type);
			long pagetotal = wd.FindWaresCount(page, pagesize, type);
			
			long pagecount = pagetotal/pagesize;
			if (pagetotal%pagesize!=0) {
				pagecount+=1;
			}
			
			
			request.setAttribute("list", list);
			request.setAttribute("pagecount", pagecount);
			request.setAttribute("page", page);
			request.setAttribute("type", type);
			request.setAttribute("warestype", "WaresListServlet");
			
			request.getRequestDispatcher("catlist.jsp").forward(request, response);
		
		
	}

}
